﻿using System;
using System.IO;
using VisionCloud.Utility;

namespace VisionCloud.Domain.DtoModel.Ms
{
    public enum VersionStatus
    { Old = 1, New = 2 }

    public class UnzipTaskDataEntity
    {
        public string FileKey { get; set; }
        public string UploadGuid { get; set; }
        public string UsbKeyNO { get; set; }

        public string TaskId { get; set; }

        public FileInfo SrcFileInfo { get; set; }

        public string DestDirPath { get; set; }

        public string CompanyNo { get; set; }
        public string ProjectNo { get; set; }
        public bool IsHaveError { get; set; }
        public string ErrorMessage { get; set; }

        public VersionStatus MyVersionStatus { get; set; }
        public string backupFileName { get; set; }

        public UnzipTaskDataEntity()
        {
        }

        public static UnzipTaskDataEntity NewObjectByNewVersion(FileInfo fileInfo, string taskReportData, string taskDataBackup)
        {
            UnzipTaskDataEntity result = new UnzipTaskDataEntity();
            try
            {
                result.MyVersionStatus = VersionStatus.New;
                var srcFileFullName = fileInfo.FullName;
                string srcFileName = Path.GetFileNameWithoutExtension(srcFileFullName);
                var array = srcFileName.Split('_');
                if (array.Length != 5)
                {
                    result.IsHaveError = true;
                    result.ErrorMessage = StartupServer.CommonLocalizer["YB0161"].Value;//"文件名称格式错误";
                    return result;
                }
                result.UsbKeyNO = array[0];
                result.CompanyNo = array[1];
                result.ProjectNo = array[2];
                result.TaskId = (new Guid(array[3])).ToString();
                result.UploadGuid = result.FileKey = Path.GetFileNameWithoutExtension(array[4]);
                result.SrcFileInfo = fileInfo;
                result.IsHaveError = false;
                result.ErrorMessage = string.Empty;
                result.DestDirPath = Path.Combine(taskReportData, result.CompanyNo, result.ProjectNo, result.TaskId, result.UploadGuid);
                string backupDir = Path.Combine(taskDataBackup, result.CompanyNo, result.ProjectNo, result.TaskId);
                result.backupFileName = Path.Combine(backupDir, result.UploadGuid + ".zip");
                return result;
            }
            catch (Exception ex)
            {
                result.IsHaveError = true;
                result.ErrorMessage = ex.ToString();
                return result;
            }
        }

        public static UnzipTaskDataEntity OldObjectByNewVersion(FileInfo fileInfo, string taskReportData, string taskDataBackup)
        {
            UnzipTaskDataEntity result = new UnzipTaskDataEntity();
            try
            {
                result.MyVersionStatus = VersionStatus.Old;
                var srcFileFullName = fileInfo.FullName;
                string srcFileName = Path.GetFileNameWithoutExtension(srcFileFullName);
                int firstPositionNum = srcFileName.IndexOf("-");
                result.UsbKeyNO = srcFileName.Substring(0, firstPositionNum);
                result.TaskId = srcFileName.Substring(firstPositionNum + 1, 36);
                result.UploadGuid = result.FileKey = srcFileName.Substring(firstPositionNum + 1 + 36 + 1, 36);
                result.SrcFileInfo = fileInfo;
                result.IsHaveError = false;
                result.ErrorMessage = string.Empty;
                result.DestDirPath = Path.Combine(taskReportData, result.UsbKeyNO, result.TaskId, result.UploadGuid);
                string backupDir = Path.Combine(taskDataBackup, result.UsbKeyNO, result.TaskId);
                result.backupFileName = Path.Combine(backupDir, result.UploadGuid + ".zip");
                return result;
            }
            catch (Exception ex)
            {
                result.IsHaveError = true;
                result.ErrorMessage = ex.ToString();
                return result;
            }
        }
    }
}
